package com.situ.springboot.controller;

import com.situ.springboot.pojo.entity.Custom;
import com.situ.springboot.pojo.entity.Service;
import com.situ.springboot.pojo.entity.User;
import com.situ.springboot.pojo.vo.RecordVO;
import com.situ.springboot.service.CustomService;
import com.situ.springboot.service.RecordService;
import com.situ.springboot.service.ServiceService;
import com.situ.springboot.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/service")
public class ServiceController {
    @Autowired
    private ServiceService serviceService;

    @Autowired
    private RecordService recordService;

    @Autowired
    private CustomService customService;

    @RequestMapping("/selectByPage")
    public String selectByPage(@RequestParam(defaultValue = "1") Integer pageNo,
                               @RequestParam(defaultValue = "10") Integer pageSize, Model model) {
        System.out.println("ServiceController.selectByPage");
        PageInfo pageInfo = serviceService.selectByPage(pageNo, pageSize);
        System.out.println("pageInfo:" + pageInfo);
        //把pageInfo数据放到内存里面
        model.addAttribute("pageInfo", pageInfo);
        //转发到service_list界面展示
        return "/service/list";
    }


    @RequestMapping("/toUpdate")
    public String toUpdate(Integer id, Model model) {
        Service service = serviceService.selectById(id);
        model.addAttribute("service", service);
        return "/service/update";
    }

    @RequestMapping("/update")
    public String update(Service service) {
        serviceService.update(service);
        return "redirect:/service/selectByPage";
    }


    @RequestMapping("/toAdd")
    public String toAdd() {
        return "/service/add";
    }

    @RequestMapping("/add")
    public String add(Service service) {
        serviceService.add(service);
        return "redirect:/service/selectByPage";
    }



    @RequestMapping("/deleteById")
    public String deleteById(Integer id) {
        serviceService.deleteById(id);
        //redirect:重定向，告诉浏览器发送请求 /service/selectByPage
        //删除完之后，应该查找展示最新的数据
        return "redirect:/service/selectByPage";
    }

    @RequestMapping("/deleteAll")
    public String deleteAll(Integer[] ids) {
        serviceService.deleteAll(ids);
        return "redirect:/service/selectByPage";
    }



    //选服务
    @RequestMapping("/toSelectService")
    @Transactional //事务
    public String toSelectService(Integer id, HttpSession session, RecordVO recordvo){
        //选择服务
        Custom custom = (Custom) session.getAttribute("custom");
        Custom custom1 = customService.selectById(custom.getId());
        Service service = serviceService.selectById(id);

        recordvo.setCustomId(custom1.getId());
        recordvo.setServiceId(service.getId());
        recordvo.setServiceMoney(service.getMoney());

        recordService.add(recordvo);
        
        //扣钱
        double finalMoney = custom1.getMoney() - service.getMoney();
        Custom newCustom = new Custom();
        newCustom.setId(custom.getId());
        newCustom.setMoney(finalMoney);
        customService.updateMoney(newCustom);

        return "redirect:/record/selectByLoginCustom";
    }


    //模糊搜索
    @RequestMapping("/SelectContent")
    public String SelectContent(String content,Model model,HttpSession session){
        List<Service> list =serviceService.selectByContent(content);
        model.addAttribute("list",list);

        // recordvos为当前登录用户的消费记录表
        Custom custom = (Custom) session.getAttribute("custom");
        List<RecordVO> recordvos = recordService.selectByCustomId(custom.getId());
        model.addAttribute("recordvos",recordvos);
        return "/service/selectcontent";
    }
}
